package com.burst.reptile.framework.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.utils.sql.SqlUtil;
import com.burst.reptile.framework.web.page.PageDomain;
import com.burst.reptile.framework.web.page.TableSupport;
import com.burst.reptile.framework.web.result.Result;


/**
 * web层通用数据处理
 * @author wujin
 * @date: 2020年5月24日 上午11:13:19
 */
public class BaseController {
	
	protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	public static final Result<Object> SUCCESS = Result.success(null);
	
	public static final String TITLE_PREFIX = "帅小铺";
	public static final String TITLE_SUFFIX = "-实体商户社群服务平台";
	public static final String TITLE = TITLE_PREFIX + TITLE_SUFFIX;

	/**
     * 获取layui table的分页参数
     * @return
     */
    public static <T> Page<T> startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
        boolean asc = pageDomain.isAsc();
        String orderByColumn = SqlUtil.escapeOrderBySql(pageDomain.getOrderByColumn());
		Page<T> page = new Page<>(pageNum, pageSize);
		page.addOrder(asc ? OrderItem.asc(orderByColumn) : OrderItem.desc(orderByColumn));
        return page;
    }
    
    /**
     * 响应返回结果
     * @param rows 影响行数
     * @return 操作结果
     */
    protected Result<Object> toResult(int rows){
        return rows > 0 ? Result.success(null) : Result.error();
    }
    
}
